package cn.wgx.module.mind.websocket.config;

import cn.wgx.module.mind.websocket.config.interceptor.*;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.*;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

import java.util.List;


/**
 * 此配置必须让springmvc扫描到
 */
@Configuration
@EnableWebSocketMessageBroker
@ConfigurationProperties(prefix = "spring.rabbitmq")
@Data
public class WebSocketStompConfig extends WebSocketMessageBrokerConfigurationSupport
        implements WebSocketMessageBrokerConfigurer{

    private String host = "127.0.0.1";
    private Integer stomport = 61613;
    private String username = "guest";
    private String password = "guest";
    private String virtual_host = "guest";

    public static final String USER_DESTINATION = "/user";

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        //设置单独发送用户消息前缀
        config.setUserDestinationPrefix(USER_DESTINATION);

        //基于内存的STOMP消息代理
//        config.enableSimpleBroker("/topic");

        //基于RabbitMQ 的STOMP消息代理
        config.enableStompBrokerRelay("/topic")
                .setVirtualHost(virtual_host)
                .setRelayHost(host)
                .setRelayPort(stomport)
                .setClientLogin(username)
                .setClientPasscode(password)
                .setSystemLogin(username)
                .setSystemPasscode(password);
        config.setApplicationDestinationPrefixes("/app");
    }

    //ws 连接地址
    public final static String PATH_CHAT = "/ws/gs-guide-websocket";

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint(PATH_CHAT)
                .setHandshakeHandler(new DefaultHandshakeHandler())
                .setAllowedOrigins("*") //跨域设置
                .withSockJS() //启用stomp
                //connect握手拦截器
                .setInterceptors(new ChatHandlerShareInterceptor())
                ;
    }

    /**
     *  stomp拦截器
     * 1、设置拦截器
     * 2、首次连接的时候，获取其Header信息，利用Header里面的信息进行权限认证
     * 3、通过认证的用户，使用 accessor.setUser(user); 方法，将登陆信息绑定在该 StompHeaderAccessor 上，在Controller方法上可以获取 StompHeaderAccessor 的相关信息
     *
     * @param registration
     */
    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.interceptors(new UserInterceptor());
    }

    @Override
    public void configureClientOutboundChannel(ChannelRegistration registration) {
//        registration.interceptors(new UserInterceptor());
    }

    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {

    }

    @Override
    public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
        return super.configureMessageConverters(messageConverters);
    }

//    @Bean
//    public WebSocketHandler subProtocolWebSocketHandler(){
//        return new SessionHandler(clientInboundChannel(), clientOutboundChannel());
//    }

}